﻿using Docker.DotNet;
using Docker.DotNet.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace docker_WebApplication.Controllers
{
    public class HomeAPIController : Controller
    {
        /// <summary>
        /// 获取docker实例list
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index1Async(string ip)
        {
            using DockerClient client = new DockerClientConfiguration(new Uri(ip))
            .CreateClient();
            IList<ContainerListResponse> containers = await client.Containers.ListContainersAsync(
                new ContainersListParameters()
                {
                    All = true
                    //Limit = 10,
                });
            foreach (var item in containers)
            {
                Console.WriteLine(item.ID);
                Console.WriteLine(item.Image);
                Console.WriteLine(item.ImageID);
                Console.WriteLine(item.Labels);
                Console.WriteLine(item.Mounts);
                Console.WriteLine(item.Names);
                Console.WriteLine(item.NetworkSettings);
                Console.WriteLine(item.Ports);
                Console.WriteLine(item.SizeRootFs);
                Console.WriteLine(item.SizeRw);
                Console.WriteLine(item.State);
                Console.WriteLine(item.Status);
                Console.WriteLine("---------------------------------");

            }
            string data = JsonConvert.SerializeObject(containers);
            return Content(data);
        }
        /// <summary>
        /// 删除docker实例
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index2Async(string ip,string id)
        {
            using DockerClient client = new DockerClientConfiguration(new Uri(ip))
                .CreateClient();
            //删除一个容器
            await client.Containers.RemoveContainerAsync(id, new ContainerRemoveParameters()
            {
                Force = true
            });
            Thread.Sleep(2000);
            return Content("已经删除");
        }
        /// <summary>
        /// 获取docker实例list
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index3Async(string ip)
        {
            using DockerClient client = new DockerClientConfiguration(new Uri(ip))
                .CreateClient();
            IList<ImagesListResponse> containers = await client.Images.ListImagesAsync(
                new ImagesListParameters()
                {
                    All = false
                });
            string data = JsonConvert.SerializeObject(containers);
            return Content(data);
        }

        /// <summary>
        /// run一个docker实例
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index4Async(string ip,string image,string port)
        {
            if (string.IsNullOrEmpty(port))
            {
                return Content("映射端口不能为空");
            }
            if (string.IsNullOrEmpty(image))
            {
                return Content("镜像不能为空");
            }

            IList<PortBinding> post = new List<PortBinding>();
            post.Add(new PortBinding()
            {
                HostIP = "",
                HostPort = port
            });
            using DockerClient client = new DockerClientConfiguration(new Uri(ip))
                .CreateClient();
            IDictionary<string, IList<PortBinding>> ports = new Dictionary<string, IList<PortBinding>>();
            ports.Add("80/tcp", post);
            CreateContainerResponse fanhui = await client.Containers.CreateContainerAsync(new CreateContainerParameters()
            {
                Image = image,
                HostConfig = new HostConfig()
                {
                    PortBindings = ports
                    //DNS = new[] { "8.8.8.8", "8.8.4.4" }
                }
            });
            Console.WriteLine(fanhui.ID);
            // 开始一个容器
            await client.Containers.StartContainerAsync(fanhui.ID, new ContainerStartParameters()
            {
                DetachKeys = ""
            });
            return Content("成功");
        }
    }
}
